﻿using Mrc.Common;
using Mrc.Config;
using Mrc.Domain.Entitity;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace Mrc.Domain.Module.ConfigCenter
{
    public class SysConfigManager
    {
        public async Task<Dictionary<string,string>> Add(SysConfig model)
        {
            var sysConfigService = new SysConfigService();
            var config = new KVConfigStoreCenter(model.Content);
            if(sysConfigService.Db.Queryable<SysConfig>().Any(x => x.ItemName == model.ItemName && x.Version == model.Version && x.FileName == model.FileName && x.Env == model.Env))
            {
                throw new MrcException("配置文件已存在");
            }
            List<SysConfig> models = new List<SysConfig>();
            foreach (var item in config.AllKV)
            {
                models.Add(new SysConfig
                {
                    Content = item.Value,
                    Env = model.Env,
                    FileName = model.FileName,
                    ItemName = model.ItemName,
                    KeyName = item.Key,
                    Version = model.Version
                });
            }
            await sysConfigService.Add(models);
            return config.AllKV;
        }
    }
}
